package org.apache.rocketmq.example.quickstart;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Producer {
	
	public static String NAMESRV_ADDR = "127.0.0.1:9876";
	public static void main(String[] args) throws MQClientException, InterruptedException {

		DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");

		producer.setNamesrvAddr(NAMESRV_ADDR);
		producer.start();

		String  body = "春江潮水连海平，海上明月共潮生。\n" +
				"\n" +
				"滟滟随波千万里，何处春江无月明！\n" +
				"\n" +
				"江流宛转绕芳甸，月照花林皆似霰；\n" +
				"\n" +
				"空里流霜不觉飞，汀上白沙看不见。\n" +
				"\n" +
				"江天一色无纤尘，皎皎空中孤月轮。\n" +
				"\n" +
				"江畔何人初见月？江月何年初照人？\n" +
				"\n" +
				"人生代代无穷已，江月年年望相似。\n" +
				"\n" +
				"不知江月待何人，但见长江送流水。\n" +
				"\n" +
				"白云一片去悠悠，青枫浦上不胜愁。\n" +
				"\n" +
				"谁家今夜扁舟子？何处相思明月楼？\n" +
				"\n" +
				"可怜楼上月徘徊，应照离人妆镜台。\n" +
				"\n" +
				"玉户帘中卷不去，捣衣砧上拂还来。\n" +
				"\n" +
				"此时相望不相闻，愿逐月华流照君。\n" +
				"\n" +
				"鸿雁长飞光不度，鱼龙潜跃水成文。\n" +
				"\n" +
				"昨夜闲潭梦落花，可怜春半不还家。\n" +
				"\n" +
				"江水流春去欲尽，江潭落月复西斜。\n" +
				"\n" +
				"斜月沉沉藏海雾，碣石潇湘无限路。\n" +
				"\n" +
				"不知乘月几人归，落月摇情满江树。";


		for (int i = 0; i < 1000000; i++) {
			try {

				Message msg = new Message("TopicTest", "TagA",
						body.getBytes(RemotingHelper.DEFAULT_CHARSET));
				msg.setDelayTimeLevel(455);
				msg.setKeys("春江花月夜 ["+i+"] ---> " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss")));
				SendResult sendResult = producer.send(msg);

				System.out.printf("%s%n", sendResult);
			} catch (Exception e) {
				e.printStackTrace();
				Thread.sleep(1000);
			}
		}

		producer.shutdown();
	}
}
